<template>

  <el-main style="height: 930px;">

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd" >新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-input style="width: 200px" placeholder="请输入供应商名称" suffix-icon="el-icon-position" class="ml-5" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>


    <div>
      <el-table
        :data="tableData"
        :cell-style="{textAlign:'center'}"
        :header-cell-style="{textAlign:'center'}"
        style="width: 100%;"
        empty-text="无"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading">
        <el-table-column type="selection" width="50" ></el-table-column>
        <el-table-column prop="name" label="供应商名称" width="120"></el-table-column>
        <el-table-column prop="address" label="地址" width="100"></el-table-column>
        <el-table-column prop="manager" label="联系人" width="160" ></el-table-column>
        <el-table-column prop="phone" label="联系电话" width="100" ></el-table-column>
        <el-table-column prop="bankName" label="开户行" width="120" ></el-table-column>
        <el-table-column prop="bankCount" label="银行账号" width="120" ></el-table-column>
        <el-table-column prop="fax" label="传真" width="160" ></el-table-column>
        <el-table-column prop="postalCode" label="邮编" ></el-table-column>
        <el-table-column prop="legalPerson" label="法人"></el-table-column>
        <el-table-column prop="notes" label="备注"></el-table-column>
        <el-table-column label="操作"  width="200" align="center">
          <template slot-scope="scope">
            <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
            <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.name)"
            >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>



    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog title="设置供应商信息" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="供应商名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人">
          <el-input v-model="form.manager" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开户行">
          <el-input v-model="form.bankName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="银行账号">
          <el-input v-model="form.bankCount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="传真">
          <el-input v-model="form.fax" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮编">
          <el-input v-model="form.postalCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="法人">
          <el-input v-model="form.legalPerson" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.notes" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="设置供应商信息" :visible.sync="dialogFormVisible1" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="供应商名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人">
          <el-input v-model="form.manager" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开户行">
          <el-input v-model="form.bankName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="银行账号">
          <el-input v-model="form.bankCount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="传真">
          <el-input v-model="form.fax" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮编">
          <el-input v-model="form.postalCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="法人">
          <el-input v-model="form.legalPerson" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.notes" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="save1">确 定</el-button>
      </div>
    </el-dialog>

  </el-main>


</template>

<script>

import axios from "axios";

export default {
  name: 'SupplierManage',
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      loading:false,
      dialogFormVisible: false,
      dialogFormVisible1:false,
      multipleSelection: [],
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      axios.post("http://127.0.0.1:8080/test/provider/getAllProviderPage", {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        name: this.name,
      }).then(res => {
        this.tableData = res.data.data.list
        this.total = res.data.date.total
      })
    },

    reset() {
      this.name = ""
      this.load()
    },

    del(name) {
      axios.get("http://127.0.0.1:8080/test/provider/delete/"+name).then(res => {
        if (res.data.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },

    save() {
      axios.post("http://127.0.0.1:8080/test/provider/register",
        {
      "name":this.form.name,
      "address":this.form.address,
      "manager":this.form.manager,
      "phone":this.form.phone,
      "bankName":this.form.bankName,
      "bankCount":this.form.bankCount,
      "fax":this.form.fax,
      "postalCode":this.form.postalCode,
      "legalPerson":this.form.legalPerson,
      "notes":this.form.notes,
        }).then(res => {
        if (res.data.code === 200) {
          this.$message.success("新增成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("新增失败")
        }
      })
      this.load();
    },

    save1() {
      axios.post("http://127.0.0.1:8080/test/provider/update",
        {
          "id":this.form.id,
          "name":this.form.name,
          "address":this.form.address,
          "manager":this.form.manager,
          "phone":this.form.phone,
          "bankName":this.form.bankName,
          "bankCount":this.form.bankCount,
          "fax":this.form.fax,
          "postalCode":this.form.postalCode,
          "legalPerson":this.form.legalPerson,
          "notes":this.form.notes,
        }).then(res => {
        if (res.data.code === 200) {
          this.$message.success("编辑成功")
          this.dialogFormVisible1 = false
          this.load()
        } else {
          this.$message.error("编辑失败")
        }
      })
      this.load();
    },


    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible1 = true
    },

    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },

    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
  }
}

</script>

<style>

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #00aaff;
}

.el-table .quehuo {
  background: #FF0000;
}

.btn1 {
  position: relative;
  right: 0.625rem;
}

</style>
